<?php defined('BASEPATH') OR exit('No direct script access allowed');

	class user_class extends CI_Model {
	
		public function insert($post) {
			$strName = stripslashes(filter_var($post['strName'], FILTER_SANITIZE_STRING));
			$dateBirthday = stripslashes(filter_var($post['dateBirthday'], FILTER_SANITIZE_STRING));
			$intAge = stripslashes(filter_var($post['intAge'], FILTER_SANITIZE_STRING));
			$strEmail = stripslashes(filter_var($post['strEmail'], FILTER_SANITIZE_STRING));
			$dateConceptionDate = stripslashes(filter_var($post['dateConceptionDate'], FILTER_SANITIZE_STRING));
			$strPassword = stripslashes(filter_var($post['strPassword'], FILTER_SANITIZE_STRING));
			//$md = md5(date('Y-m-d H:i:s').$strName.$dateBirthday);
			//$strPassword = substr($md, 5, 6);
			$data = array(
			'strName' => $strName, 
			'dateBirthday' => $dateBirthday, 
			'intAge' => $intAge,
			'strEmail' => $strEmail,
			'dateConceptionDate' => $dateConceptionDate,
			'strPassword' => pw_hash($strPassword),
			'intDisabled' => 0,
			'strCreatedby'=>$this->session->userdata('intID'),
			'dateCreatedDate'=>date('Y-m-d H:i:s'),
			'strModifiedBy'=>$this->session->userdata('intID'),
			'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
			
			$this->db->insert('jtn_users', $data); 
			
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully registered.";
				$message['intID'] = $this->db->insert_id();
				$message['strEmail'] = $strEmail;
				$message['strKeys'] = pw_hash($strPassword);
				$message['strName'] = $strName;

				$dataPoints1 = array(
						'intUserID' => $this->db->insert_id(),
						'intPointsID' => 1,
						'intPoints' => 0,
						'intDisabled' => 0,
						'strCreatedby'=>$this->session->userdata('intID'),
						'dateCreatedDate'=>date('Y-m-d H:i:s'),
						'strModifiedBy'=>$this->session->userdata('intID'),
						'dateModifiedDate'=>date('Y-m-d H:i:s')
				);
				$dataPoints2 = array(
						'intUserID' => $this->db->insert_id(),
						'intPointsID' => 2,
						'intPoints' => 0,
						'intDisabled' => 0,
						'strCreatedby'=>$this->session->userdata('intID'),
						'dateCreatedDate'=>date('Y-m-d H:i:s'),
						'strModifiedBy'=>$this->session->userdata('intID'),
						'dateModifiedDate'=>date('Y-m-d H:i:s')
				);
				$dataPoints3 = array(
						'intUserID' => $this->db->insert_id(),
						'intPointsID' => 3,
						'intPoints' => 0,
						'intDisabled' => 0,
						'strCreatedby'=>$this->session->userdata('intID'),
						'dateCreatedDate'=>date('Y-m-d H:i:s'),
						'strModifiedBy'=>$this->session->userdata('intID'),
						'dateModifiedDate'=>date('Y-m-d H:i:s')
				);
				$dataPoints4 = array(
						'intUserID' => $this->db->insert_id(),
						'intPointsID' => 4,
						'intPoints' => 0,
						'intDisabled' => 0,
						'strCreatedby'=>$this->session->userdata('intID'),
						'dateCreatedDate'=>date('Y-m-d H:i:s'),
						'strModifiedBy'=>$this->session->userdata('intID'),
						'dateModifiedDate'=>date('Y-m-d H:i:s')
				);
				$this->db->insert('jtn_users_points', $dataPoints1);
				$this->db->insert('jtn_users_points', $dataPoints2);
				$this->db->insert('jtn_users_points', $dataPoints3);
				$this->db->insert('jtn_users_points', $dataPoints4);
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;			
			}
			return $message;			
		}

		public function chk_email($post){
			$strEmail = stripslashes(filter_var($post['strEmail'], FILTER_SANITIZE_STRING));
			$query = $this->db->get_where('jtn_users',array('intDisabled'=>0,'strEmail'=>$strEmail))->result_array();
			$query = current($query);
				
			if(trim($query['intID'])=="") {
				return 0;
			} else {
				return $query;
			}
		}		

		/*
		 Get User DATA
		*/
		public function get_user_data($post){
			if(isset($post['intUserID'])){
				$intUserID = stripslashes(filter_var($post['intUserID'], FILTER_SANITIZE_STRING));
				$this->db->where(array('intID'=>$intUserID));
			}
			if(isset($post['intID'])){
				$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
				$this->db->where(array('intID'=>$intID));
			}
			$query = $this->db->get('jtn_users')->result_array();
			$query = current($query);
		
			if(trim($query['intID'])=="") {
				return 0;
			} else {
				return $query;
			}
		}

		public function count_client_user($post){
			$this->db->where(array('intAccountType'=>0));
			$query = $this->db->get('jtn_users');
			$rowcount = $query->num_rows();
			return $rowcount;
		}
		
		public function list_users_data($post){
			if(isset($post['intUserID'])){
				$intUserID = stripslashes(filter_var($post['intUserID'], FILTER_SANITIZE_STRING));
				$this->db->order_by("intID", "asc");
				$query = $this->db->get_where('jtn_users',array('intAccountType'=>0,'intDisabled'=>0,'intID'=>$intUserID));
			} else {
				$this->db->order_by("intID", "asc");
				$query = $this->db->get_where('jtn_users',array('intAccountType'=>0,'intDisabled'=>0));
			}
			if ($query->num_rows() > 0){
				$result = $query->result_array();
				return $result;
			}
			else{
				return 0;
			}
		}		
		public function update($post) {
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
			//$strUserPoint = stripslashes(filter_var($post['strUserPoint'], FILTER_SANITIZE_STRING));
			$strUserName = stripslashes(filter_var($post['strUserName'], FILTER_SANITIZE_STRING));
			$strBirthDay = stripslashes(filter_var($post['strBirthDay'], FILTER_SANITIZE_STRING));
			$strUserEmail = stripslashes(filter_var($post['strUserEmail'], FILTER_SANITIZE_STRING));
			$strImgLink = stripslashes(filter_var($post['strImgLink'], FILTER_SANITIZE_STRING));
			$ageUser = stripslashes(filter_var($post['ageUser'], FILTER_SANITIZE_STRING));
			$strPassword = pw_hash(stripslashes(filter_var($post['strPassword'], FILTER_SANITIZE_STRING)));
		
			$data = array(
					'strName' => $strUserName,
					'dateBirthday' => $strBirthDay,
					'strEmail' => $strUserEmail,
					'strImgLink' => $strImgLink,
					'intAge' => $ageUser,
					'strPassword' => $strPassword,
					'dateCreatedDate'=>date('Y-m-d H:i:s'),
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
		
			$this->db->where(array('intID'=>$intID));
			$this->db->update('jtn_users', $data);
				
			//$this->db->where(array('intUserID'=>$intID));
			//$this->db->update('jtn_users_points',array('intPoints'=>$strUserPoint));
				
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully saved.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}		
		public function add_user($post) {
			//$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
			//$strUserPoint = stripslashes(filter_var($post['strUserPoint'], FILTER_SANITIZE_STRING));
			$strUserName = stripslashes(filter_var($post['strUserName'], FILTER_SANITIZE_STRING));
			$strBirthDay = stripslashes(filter_var($post['strBirthDay'], FILTER_SANITIZE_STRING));
			$strUserEmail = stripslashes(filter_var($post['strUserEmail'], FILTER_SANITIZE_STRING));
			$strImgLink = stripslashes(filter_var($post['strImgLink'], FILTER_SANITIZE_STRING));
			$strPassword = pw_hash(stripslashes(filter_var($post['strPassword'], FILTER_SANITIZE_STRING)));
			$ageUser = stripslashes(filter_var($post['ageUser'], FILTER_SANITIZE_STRING));
		
			$data = array(
					'strName' => $strUserName,
					'dateBirthday' => $strBirthDay,
					'strEmail' => $strUserEmail,
					'strImgLink' => $strImgLink,
					'strPassword' => $strPassword,
					'intAge' => $ageUser,
					'dateCreatedDate'=>date('Y-m-d H:i:s'),
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
			$this->db->insert('jtn_users', $data);
				
			$lastID = $this->db->insert_id();
		
			$data2 = array(
					'intUserID' => $lastID,
					'intPoints' => 0,
					'intPointsID'=> 1,
					'strCreatedBy'=>$this->session->userdata('intID'),
					'dateCreatedDate'=>date('Y-m-d H:i:s'),
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
			$this->db->insert('jtn_users_points', $data2);
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully saved.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}		

		public function delete($post) {
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
		
			$data = array(
					'intDisabled' => 1,
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
		
			$this->db->where(array('intID'=>$intID));
			$this->db->update('jtn_users', $data);
		
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully deleted.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}

		public function add_user_points($post){
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
			$intPoints = stripslashes(filter_var($post['strUserPoint'], FILTER_SANITIZE_STRING));
				
			$data = array(
					'intUserID'=>$intID,
					'intPoints'=>$intPoints,
					'intPointsID'=> 1,
					'dateCreatedDate'=>date('Y-m-d H:i:s'),
					'strCreatedBy'=>$this->session->userdata('intID'),
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
						
			);
				
			$this->db->insert('jtn_users_points',$data);
				
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully deleted.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}
		public function change_perm($post){
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
		
			$data = array(
					'intAccountType' => 1,
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
		
			$this->db->where(array('intID'=>$intID));
			$this->db->update('jtn_users', $data);
		
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully deleted.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}
		public function update_user_photo($post){
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
			$strImgLink = stripslashes(filter_var($post['strImgLink'], FILTER_SANITIZE_STRING));
			
			$data = array(
					'strImgLink' => $strImgLink,
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
			
			$this->db->where(array('intID'=>$intID));
			$this->db->update('jtn_users', $data);
			
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully updated.";
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}		
	} // end class
?>